/**
 * 对微信小程序上传文件接口进行封装
 *
 * TODO 未完善
 */
import { error, defaultValUndefined, checkUndefined } from './utils'

const app = getApp()

/**
 * 上传约拍图片接口
 * @param {*} params
 */
const upload = function(req) {
  return new Promise(function(resolve, reject) {
    wx.uploadFile({
      url: checkUndefined(req.url),
      filePath: checkUndefined(req.filePath),
      name: checkUndefined(req.name),
      header: { Authorization: defaultValUndefined(app.globalData.token, '') },
      formData: defaultValUndefined(req.formData, {}),
      success: function(res) {
        console.log(res)
        if (res.statusCode == 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      },
      fail: function(res) {
        reject(res)
      }
    })
  })
}

export default upload
